package com.itheima.studentsys;

import com.itheima.Domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentTest {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        lo:
        while (true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");
            String s = sc.next();
            switch (s) {
                case "1":
                    add(list);      //System.out.println("添加学生");
                    break;
                case "2":
                    remove(list);          //System.out.println("删除学生");//根据学号删除
                    break;
                case "3":
                    updateStudent(list);      //System.out.println("修改学生");
                    break;
                case "4":
                    update(list);          //System.out.println("查看学生");
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break lo;
                default:
                    System.out.println("您的输入有误");
                    break;
            }
        }
    }

    public static void add(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        int age = sc.nextInt();
        String id;
        while (true) {
            System.out.println("请输入学生学号：");
            id = sc.next();
            int index = getIndex(list, id);
            if (index != -1) {
                System.out.println("学号重复请重新录入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生生日：");
        String birthday = sc.next();
        Student s = new Student(name, age, id, birthday);
        list.add(s);        //封装成集合
        System.out.println("添加成功");

    }//添加学生信息

    public static void update(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("未录入信息，请重新录入：");
            return;
        }
        System.out.println("姓名\t年龄\t学号\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getName() + "\t" + s.getAge() + "\t" + s.getId() + "\t" + s.getBirthday());
        }
    }//查看成员信息

    public static int getIndex(ArrayList<Student> list, String id) {
        int index = -1;                         //假设索引不存在
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if (id.equals(s.getId())) {
                index = i;          //学号对应索引位置
            }
        }
        return index;
    }//查询学号是否存在，-1不存在。

    public static void remove(ArrayList<Student> list) {
        while (true) {
            System.out.println("请输入你要删除的学生学号：");
            System.out.println("退出请输入0:");
            Scanner sc = new Scanner(System.in);
            String s = sc.next();
            if (s == "0") {
                break;
            }
            int index = getIndex(list, s);
            if (index == -1) {
                System.out.println("该学生未录入，请重新输入学号");
            } else {
                list.remove(index);
                System.out.println("删除成功");
                break;
            }
        }
    }//根据学号进行删除；
    public static void updateStudent(ArrayList<Student> list){
        System.out.println("请输入您要修改的学生学号：");
        Scanner sc=new Scanner(System.in);
        String s=sc.next();
        int index=getIndex(list,s);
        if(index==-1){
            System.out.println("要修改的信息不存在，请重新输入");
        }else {
            System.out.println("请输入新学生的姓名：");
            String name=sc.next();
            System.out.println("请输入新学生的年龄：");
            int age=sc.nextInt();
            System.out.println("请输入新学生的学号：");
            String id=sc.next();
            System.out.println("请输入新学生的生日：");
            String birthday=sc.next();
            Student stu=new Student(name,age,id,birthday);
            list.set(index,stu);
            System.out.println("修改成功");
        }
    }//修改学生信息
}
